package system

import (
	"parking-lot-go-dingding/model"
	"time"
)

type DingtalkSystemAdminStruct struct {
	Id        int       `json:"id" gorm:"type:int(11) auto_increment; NOT NULL; primary_key"`
	CorpId    string    `json:"corp_id" gorm:"type:varchar(64); NOT NULL; COMMENT:'公司id'"`
	Userid    string    `json:"userid" gorm:"type:varchar(64); NOT NULL; COMMENT:'用户id'"`
	CreatedAt time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt time.Time `json:"updated_at" gorm:"type:datetime"`
}

func (c *DingtalkSystemAdminStruct) TableName() string {
	return "dingtalk_system_admin"
}

func (c *DingtalkSystemAdminStruct) Create(corpId, userid string) error {
	db := model.Db
	if err := db.Table(c.TableName()).
		Create(map[string]interface{}{
			"corp_id":    corpId,
			"userid":     userid,
			"created_at": time.Now(),
			"updated_at": time.Now(),
		}).Error; err != nil {
		return err
	}
	return nil
}

func (c *DingtalkSystemAdminStruct) Delete(corpid string) error {
	db := model.Db
	return db.Table(c.TableName()).Where("corp_id", corpid).Delete(&DingtalkSystemAdminStruct{}).Error
}

func (c *DingtalkSystemAdminStruct) First(corpid, userId string) (DingtalkSystemAdminStruct, error) {
	var dingtalkSystemAdminStruct DingtalkSystemAdminStruct
	db := model.Db
	if err := db.
		Table(c.TableName()).
		Where("corp_id", corpid).
		Where("userid", userId).
		First(&dingtalkSystemAdminStruct).
		Error; err != nil {
		return dingtalkSystemAdminStruct, err
	}
	return dingtalkSystemAdminStruct, nil

}
